import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { FormDynamicEntity } from './form.dynamic';

/**
 * 动态表单服务
 */
@Injectable()
export class FormDynamicService {
    constructor() {

    }

    toFormGroup(forms: FormDynamicEntity<any>[]) {
        let group: any = {};

        forms.forEach(form => {
            group[form.key] = form.required ? new FormControl(form.value || '', Validators.required)
                : new FormControl(form.value || '');
        });
        return new FormGroup(group);
    }

}
